package com.xydtech.common.utils.html;

import com.spire.pdf.graphics.PdfMargins;
import com.spire.pdf.htmlconverter.qt.HtmlConverter;
import com.spire.pdf.htmlconverter.qt.Size;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;

public class ConvertHtmlStringToPdf {

    public static void main2(String[] args) throws IOException {
        //com.spire.license.LicenseProvider.setLicenseKey("9Xi8Pr+BDKbak4JaAQDFDuLwlioggGIMWyOgU0M421bM5tc5IjDxfxe2NqzmkF4WiRrlH8wzx4+rCoLWfl66DBCr2YPUfjOjFuhAXiuydNfG8HGN4jdMvFXFdhYuBnkon+79AySkSnCN5yomWzOnk25DkPT6P7ER1Ab74eLR1D3EjySC29nSkwE322K9Rv0sHsE7HKE9CckcsYIKluE7haVnB+HURwIZcb+UnK1QsGCPb45X/v7JuOTHt2X5D9NdslnkVKe/9T5vMBBBVGGTOYNhIZTquLgCeWL9o0xrZygIDXVM34SWnMa7vpp1fJt9CfY/+aXNgBGgAN1sHaHoHvpS5F9m9gv62MRKmsct6QgAiOfNWQGQ+jimhMKogrC7PjL3gc3y1aIqNRCovltcVV1D8oQq4yjKf06l3mQZnHpFiY8/n40L4v6V9S0wVvKj2vZaLH7OQ+Hf2nwJG6TvEFa3wQ/WGoDiN+UqewJZ/dqp3KwM1h0lAFCTIppKM8zJPAYyvbHhQIEsqcEIKF6gp8DW02/YRm/KMzI6a4k40a5ALHexicNEs6u6umCGLn37Cx1/trNREqPEjMt59MH3FieF+v0XmBvrwfKyHQgq8yt6Uisi/Rnvq094PIKaEgJtmcq8eFH0jjwIP/D9DUjHLTY617gzrQCUSkWxf4F8MXBeOcCSM2e5NZyTjhMFpq7i8dOaFr8pwUIIhwGuUKIU6IuHulRLZG1mbKFYt3cTwOTDxEGaUep/aLBMzj7BwbNwcosRFKE7oWpVgcUfd2dStYHhtiuWdQKCSHa/85zqqejz6cs2Duzbby7DOFNxA22S2dWv7r3tdsZPtdt1n5AHku5SQh0A+kwNPQGWUcF8IafQSRFOw0pDOJIGWEqipeUyj2M4eGKVhoC7OeUvSp8Zb5FZo8EfyiPjKaQO8Pm7HgarNL/HYZ29gm3qDcHLpMLUtUcBqyY6uIiH6Q1+i6SA0pexvFN2vF6t/KXo+70Vj5D91lGBQ9mlH0XkohbhHCqwsXBgxTKoxn1vTknhdudwMUxdaSxg4/OPwe/C/U3SnDczqMoFUpsQ2KzFCRMNgtszVtAGQgfQ9qpNxu4fPYTPQOTAl30Pefd1QjC8+GGiR+ZLRO652UBv2ZSK2ZCXcrjmgc2fHJkgxIMkUeAB1E7AR5kWsTc9iL4x/tiFFVqRe9qgEoMyDwBzkqlHHt589r3c3BeWFt8mcFEAsqZyL+NyuSTH5fL5LoildLLsKBXCzWRy0czvXG3OVXOIjX4yO1C8DhaK/07zs+ycG8KqgSD7gZdBA6KAwOKkiOmVV50SkIqcOPjRrCfS881Q6djHwHjbuwRFWr3pz1hHtN7mzhJdh+MgsAv7eByvy99ozs7hP65kCbTZJ0AV5ya3KReT10U5R5iNpkGUwgvA2TPZ7cmv9Q0sIs30xfNVr+YKonxceZYAE2+oGjsyFF9vnDmI1F0FNC8EIGjMWAkFojl7DKqOv17CdTGBFWaL");
        ////调用自定义方法HtmlToString()将HTML文件转换为字符串
        //String htmlString = HtmlToString("D:/刘湖.html");
        ////截取_之后字符串
        //String str1 = htmlString.substring(0, htmlString.indexOf("<table"));
        //String str2 = htmlString.substring(str1.length() + 1, htmlString.length());
        try {
            //File file = new File("D:/test.txt");
            //if (!file.exists()) {
            //    file.createNewFile();
            //}
            //FileWriter fileWriter = new FileWriter(file.getAbsoluteFile());
            //BufferedWriter bw = new BufferedWriter(fileWriter);
            //bw.write(str2);
            //bw.close();
            // java8以后
            List<String> ids = Files.lines(Paths.get("D:/test.txt")).collect(Collectors.toList());
            for (int i = 0; i < ids.size(); i++) {
                if (ids.get(i).contains("报告编号")) {
                    String id1 = ids.get(i).substring(0, ids.get(i).indexOf("："));
                    System.out.println(ids.get(i).substring(id1.length() + 1, ids.get(i).length()).replace("</td>", ""));
                } else if (ids.get(i).contains("报告时间")) {
                    String id1 = ids.get(i).substring(0, ids.get(i).indexOf("："));
                    System.out.println(ids.get(i).substring(id1.length() + 1, ids.get(i).length()).replace("</td>", ""));
                } else if (ids.get(i).contains("报告时间")) {
                    String id1 = ids.get(i).substring(0, ids.get(i).indexOf("："));
                    System.out.println(ids.get(i).substring(id1.length() + 1, ids.get(i).length()).replace("</td>", ""));
                } else if (ids.get(i).contains("被查询者姓名")) {
                    System.out.println(ids.get(i + 7).replace("<td class=\"f-bor-col-blue\">", "").replace("</td>", "").trim());
                    System.out.println(ids.get(i + 8).replace("<td class=\"f-bor-col-blue\">", "").replace("</td>", "").trim());
                    System.out.println(ids.get(i + 9).replace("<td class=\"f-bor-col-blue\">", "").replace("</td>", "").trim());
                    System.out.println(ids.get(i + 10).replace("<td class=\"f-bor-col-blue\">", "").replace("</td>", "").trim());
                    System.out.println(ids.get(i + 11).replace("<td class=\"f-bor-col-blue\">", "").replace("</td>", "").trim());
                } else if (ids.get(i).contains("异议信息提示")) {
                    System.out.println(ids.get(i + 3).replace("<td class=\"f-bor-col-blue\">", "").replace("</td>", "").trim());
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //将HTML文件转换为字符串
    public static String HtmlToString(String filePath) throws IOException {
        String path = filePath;
        File file = new File(path);
        FileReader fileReader = new FileReader(file);
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        StringBuilder stringBuilder = new StringBuilder();
        String temp = "";
        while ((temp = bufferedReader.readLine()) != null) {
            stringBuilder.append(temp + "\n");
        }
        bufferedReader.close();
        String str = stringBuilder.toString();
        return str;
    }

    public static void mains(String[] args) {
        com.spire.license.LicenseProvider.setLicenseKey("9Xi8Pr+BDKbak4JaAQDFDuLwlioggGIMWyOgU0M421bM5tc5IjDxfxe2NqzmkF4WiRrlH8wzx4+rCoLWfl66DBCr2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IMkUeAB1E7AR5kWsTc9iL4x/tiFFVqRe9qgEoMyDwBzkqlHHt589r3c3BeWFt8mcFEAsqZyL+NyuSTH5fL5LoildLLsKBXCzWRy0czvXG3OVXOIjX4yO1C8DhaK/07zs+ycG8KqgSD7gZdBA6KAwOKkiOmVV50SkIqcOPjRrCfS881Q6djHwHjbuwRFWr3pz1hHtN7mzhJdh+MgsAv7eByvy99ozs7hP65kCbTZJ0AV5ya3KReT10U5R5iNpkGUwgvA2TPZ7cmv9Q0sIs30xfNVr+YKonxceZYAE2+oGjsyFF9vnDmI1F0FNC8EIGjMWAkFojl7DKqOv17CdTGBFWaL");

        //指定URL路径
        String url = "file:///C:/Users/JHON/Desktop/%E6%B5%8B%E8%AF%95%E6%96%87%E4%BB%B6/%E7%8E%8B%E5%BB%BA%E5%B9%B3.html";

        //指定输出文档路径
        String fileName = "D:\\UrlToPdf.pdf";

        //指定插件路径
        String pluginPath = "D:\\plugins-windows-x64\\plugins";

        //设置插件路径
        HtmlConverter.setPluginPath(pluginPath);

        //将URL转换为PDF
        HtmlConverter.convert(url, fileName, true, 1000000, new Size(1200f, 1000f), new PdfMargins(0));
    }

    public static void main(String[] args) {
        long justNow = System.currentTimeMillis();
        int num = (int) (Math.random() * 9);
        StringBuilder s = new StringBuilder();
        Long a = Long.parseLong(s.append(num).append(justNow).toString());
        System.out.println(a);
    }
}